package com.hwy.struts.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class StrutsServletAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	private HttpServletRequest request;

	// 完全解耦合的方式使用ServletApi
	public String demo1() {
		ActionContext aContext = ActionContext.getContext();
		System.out.println("123123");
		return NONE;
	}
	
	// 使用 ServletActionContext
	public String demo2() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String name = request.getParameter("name");
		request.getSession().setAttribute("demo", 123);
		System.out.println("---获取到的值---"+name);
		return NONE;
	}
	
	// 接口注入的方式获取到request对象
	public String demo3() {
		String name = this.request.getParameter("name");
		System.out.print("----接口注入的方式--"+name);
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
	}
}
